package com.example.administrator.fangbook.importlocal.fragment.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.fangbook.R;
import com.example.administrator.fangbook.appliction.HtlApplication;
import com.example.administrator.fangbook.importlocal.bean.FileNode;
import com.example.administrator.fangbook.importlocal.utils.FileType;
import com.example.administrator.fangbook.importlocal.utils.FileValueUtil;

import java.sql.SQLException;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;


public class CapacityAdapter extends RecyclerView.Adapter<CapacityAdapter.CapacityVH> {

    private final Activity mContext;
    private ArrayList<FileNode> mFileNodes;
    private final LayoutInflater mLayoutInflater;
    private OnItemClickLisener mOnItemClickLisener;

    public CapacityAdapter(Activity activity) {
        this.mContext = activity;
        this.mLayoutInflater = LayoutInflater.from(activity);
    }

    @Override
    public CapacityVH onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CapacityVH(mLayoutInflater.inflate(R.layout.import_capacity_item, null));
    }

    @Override
    public void onBindViewHolder(final CapacityVH holder, int position) {
        FileNode preFileNode = null;
        FileNode currFileNode = null;
        if (position == 0) {
            preFileNode = null;
            currFileNode = mFileNodes.get(position);
        } else {
            preFileNode = mFileNodes.get(position - 1);
            currFileNode = mFileNodes.get(position);
        }
        if (preFileNode == null || preFileNode.getFirstChar() != currFileNode.getFirstChar()) {
            holder.itemIntelligentCharView.setVisibility(View.VISIBLE);
            holder.itemIntelligentCharLineView.setVisibility(View.VISIBLE);
            holder.itemIntelligentCharView.setText(Character.toString(currFileNode.getFirstChar()));
        } else {
            holder.itemIntelligentCharView.setVisibility(View.GONE);
            holder.itemIntelligentCharLineView.setVisibility(View.GONE);
        }
        if (currFileNode.getType() == FileType.TXT) {
            holder.ivItemIntelligentFileType.setBackgroundResource(R.drawable.ic_file_type_txt);
        } else {
            holder.ivItemIntelligentFileType.setBackgroundResource(R.drawable.ic_file_type_zwsc);
        }

        holder.itemIntelligentFilenameView.setText(currFileNode.getFullName());
        holder.tvItemIntelligentFileSize.setText(FileValueUtil.getPrintSize(currFileNode.getFile().length()));
        holder.itemIntelligentAddedTag.setVisibility(getAdd(currFileNode) ? View.VISIBLE : View.GONE);

        final FileNode finalCurrFileNode = currFileNode;
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickLisener != null) {
                    if (holder.itemIntelligentAddedTag.getVisibility() == View.GONE) {
                        mOnItemClickLisener.onItemClick(finalCurrFileNode);
                    }
                }
            }
        });
    }

    private boolean getAdd(FileNode currFileNode) {
        boolean isExist = false;
        try {
            isExist = HtlApplication.getApplication().getmDataHelper().getShelfBookDao().idExists(currFileNode.getPath());
        } catch (SQLException e) {
            e.printStackTrace();
            isExist = false;
        }
        return isExist;
    }

    @Override
    public int getItemCount() {
        return mFileNodes == null ? 0 : mFileNodes.size();
    }

    public void setList(ArrayList<FileNode> mFileNodes) {
        this.mFileNodes = mFileNodes;
        notifyDataSetChanged();
    }

    public static class CapacityVH extends RecyclerView.ViewHolder {
        @Bind(R.id.item_intelligent_char_view)
        TextView itemIntelligentCharView;
        @Bind(R.id.item_intelligent_char_line_view)
        View itemIntelligentCharLineView;
        @Bind(R.id.iv_item_intelligent_file_type)
        ImageView ivItemIntelligentFileType;
        @Bind(R.id.item_intelligent_added_tag)
        TextView itemIntelligentAddedTag;
        @Bind(R.id.item_intelligent_filename_view)
        TextView itemIntelligentFilenameView;
        @Bind(R.id.tv_item_intelligent_file_size)
        TextView tvItemIntelligentFileSize;

        public CapacityVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setOnItemClickLisener(OnItemClickLisener l) {
        this.mOnItemClickLisener = l;
    }

    public interface OnItemClickLisener {
        void onItemClick(FileNode finalCurrFileNode);
    }

}
